package com.lenovo.smarttraffic.zm_user_module;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.CollectionUserBean;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * author  张猛
 **/
public class Collection_Adapter extends RecyclerView.Adapter<Collection_Adapter.Holder> {
    private Context mContext;
    private List<CollectionUserBean> mdata;
    private DragLayout dragLayout;
    private String TAG = "Collection_Adapter";


    public Collection_Adapter(Context mContext, List<CollectionUserBean> mdata) {
        this.mContext = mContext;
        this.mdata = mdata;

//        Collections.sort(adapterBeanList, new Comparator<AdapterBean>() {
//            @Override
//            public int compare(AdapterBean t0, AdapterBean t1) {
//                return (t0.getTime().compareTo(t1.getTime()));
//            }
//        });
        Collections.sort(mdata, new Comparator<CollectionUserBean>() {
            @Override
            public int compare(CollectionUserBean t0, CollectionUserBean t1) {
                return t0.getInsertdata().compareTo(t1.getInsertdata());
            }
        });
    }


    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.rv_item_user_collection, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        holder.tv_item2_name.setText("" + mdata.get(position).getPname());
        holder.tv_item2_username.setText("" + mdata.get(position).getUsername());
        holder.tv_item2_tell.setText("" + mdata.get(position).getPtel());
        holder.tv_item2_power.setText("一般管理员" + mdata.get(position).getPsex());
        holder.rv_tv_time.setText("" + mdata.get(position).getInsertdata());
        if (mdata.get(position).getPsex().equals("男")) {
            holder.circleImageView2.setImageResource(R.mipmap.touxiang_2);
        } else {
            holder.circleImageView2.setImageResource(R.mipmap.touxiang_1);
        }

        if (mdata.get(position).getZhiding()) {
            dragLayout.setBackgroundColor(Color.parseColor("#484D47"));
            holder.tv_item2_on1.setText("取消置顶");
            holder.tv_item2_on2.setText("取消收藏");
            holder.tv_item2_on1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onLis.untop(holder.getLayoutPosition(), dragLayout);
                }
            });
            holder.tv_item2_on2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onLis.delete(holder.getLayoutPosition(), dragLayout);
                }
            });
        } else {
            holder.tv_item2_on1.setText("取消收藏");
            holder.tv_item2_on2.setText("置顶");
            dragLayout.setBackgroundColor(Color.parseColor("#ffffff"));
            holder.tv_item2_on1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onLis.delete(holder.getLayoutPosition(), dragLayout);
                }
            });
            holder.tv_item2_on2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onLis.onTop(holder.getLayoutPosition(), dragLayout);
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        return mdata.size();
    }


    class Holder extends RecyclerView.ViewHolder {
        TextView tv_item2_name, tv_item2_tell, tv_item2_username, tv_item2_on2, tv_item2_power, tv_item2_intent, tv_item2_on1, rv_tv_time;
        CircleImageView circleImageView2;
        public Holder(View itemView) {
            super(itemView);
            tv_item2_name = itemView.findViewById(R.id.tv_item2_name);
            tv_item2_tell = itemView.findViewById(R.id.tv_item2_tell);
            tv_item2_username = itemView.findViewById(R.id.tv_item2_username);
            circleImageView2 = itemView.findViewById(R.id.iv_item2_avatar);
            tv_item2_on1 = itemView.findViewById(R.id.tv_item2_on1);
            tv_item2_on2 = itemView.findViewById(R.id.tv_item2_on2);
            tv_item2_power = itemView.findViewById(R.id.tv_item2_power);
            tv_item2_intent = itemView.findViewById(R.id.tv_item2_intent);
            rv_tv_time = itemView.findViewById(R.id.rv_tv_time);
            dragLayout = itemView.findViewById(R.id.dragLayout2);
        }
    }


    public interface OnLis {
        void onTop(int position, DragLayout dragLayout);

        void insert(int position, DragLayout dragLayout);

        void delete(int position, DragLayout dragLayout);

        void untop(int position, DragLayout dragLayout);
    }

    public OnLis onLis;

    public void setOnLis(OnLis onLis) {
        this.onLis = onLis;
    }


}
